package cho114366.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;
import javax.swing.JCheckBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.ComboBoxModel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JList;
import javax.swing.JScrollPane;

import java.awt.Dimension;

import javax.swing.AbstractListModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtBtn;
	private JPasswordField pwdBtn;
	private JList list1 = new JList();
	private JList list2 = new JList();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle("기본 컴퍼넌트");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		FlowLayout fl_contentPanel = new FlowLayout();
		fl_contentPanel.setHgap(10);
		contentPanel.setLayout(fl_contentPanel);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton imgBtn = new JButton("이미지 버튼");
			imgBtn.setVerticalAlignment(SwingConstants.TOP);
			imgBtn.setToolTipText("이미지 버튼입니다");
			imgBtn.setFont(new Font("휴먼매직체", Font.PLAIN, 16));
			imgBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			imgBtn.setIcon(new ImageIcon(BComp.class
					.getResource("/cho114366/swing/icon/icon2.png")));
			contentPanel.add(imgBtn);
		}
		{
			JLabel imgLabel = new JLabel("이미지레이블");
			imgLabel.setIcon(new ImageIcon(BComp.class
					.getResource("/cho114366/swing/icon/icon2.png")));
			imgLabel.setFont(new Font("휴먼매직체", Font.PLAIN, 16));
			contentPanel.add(imgLabel);
		}
		{
			JCheckBox chkSuji = new JCheckBox("수지");
			chkSuji.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if (c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText("");

				}
			});
			chkSuji.setFont(new Font("휴먼매직체", Font.PLAIN, 12));
			contentPanel.add(chkSuji);
		}
		{
			JCheckBox chkHye = new JCheckBox("혜교");
			chkHye.setFont(new Font("휴먼매직체", Font.PLAIN, 12));
			contentPanel.add(chkHye);
		}
		{
			JCheckBox chkHyun = new JCheckBox("지현");
			chkHyun.setFont(new Font("휴먼매직체", Font.PLAIN, 12));
			contentPanel.add(chkHyun);
		}

		// 라디오 버튼 선언 시작
		JRadioButton radSuji = new JRadioButton("수지");
		radSuji.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JRadioButton r = (JRadioButton) e.getSource(); // 형변환 해 주는 거지
				if (r.isSelected()) {
					JOptionPane.showMessageDialog(null, "수지가 선택되었어요!!");
				}
			}
		});
		contentPanel.add(radSuji);
		// 디자인 가서 이벤트 발생은 시키고, 여기 와서 나머지 코드 완성 시키자

		JRadioButton radHye = new JRadioButton("혜교");
		radHye.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {

			}
		});
		contentPanel.add(radHye);

		JRadioButton radHyun = new JRadioButton("지현");
		radHyun.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				
			}
		});
		contentPanel.add(radHyun);
		// 라디오 버튼 선언 끝
		// 라디오 버튼 그룹 생성 및 추가 시작
		ButtonGroup rgroup = new ButtonGroup();
		rgroup.add(radSuji);
		rgroup.add(radHye);
		rgroup.add(radHyun);
		{
			txtBtn = new JTextField();
			contentPanel.add(txtBtn);
			txtBtn.setColumns(10);
		}
		{
			JButton txt1Btn = new JButton("입력문자보기");
			txt1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, txtBtn.getText());
					// textField에 입력된 문자 출력
				}
			});
			contentPanel.add(txt1Btn);
		}
		{
			pwdBtn = new JPasswordField();
			pwdBtn.setEchoChar('♥');
			pwdBtn.setColumns(16);
			contentPanel.add(pwdBtn);
		}
		{
			JButton pwd1Btn = new JButton("입력문자보기");
			pwd1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, pwdBtn.getPassword());
				}
			});
			contentPanel.add(pwd1Btn);
		}
		{
			JScrollPane scrollPane1 = new JScrollPane();
			scrollPane1.setPreferredSize(new Dimension(200, 200));
			contentPanel.add(scrollPane1);
			{
				//JList에 추가할 할목을 배열로 주입
				String[] data =
					{"수지","혜교","지현","사람","기름","열정","사랑","하라","ㄴㄴㄴ","ㄴㅇㅁㅇ","ㅁㄴㅇㅁㄴㅇ"};
				
				list1 = new JList(data);
				list1.setToolTipText("");
				scrollPane1.setViewportView(list1);
			}
		}
		{
			JScrollPane scrollPane2 = new JScrollPane();
			scrollPane2.setPreferredSize(new Dimension(100, 100));
			contentPanel.add(scrollPane2);
			{
				
				list2 = new JList();
				list2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if(e.getClickCount()==2)
							{JOptionPane.showMessageDialog(null,list2.getSelectedIndex()+"/"+list2.getSelectedValue());}
					}
				});
			list2.setModel(new AbstractListModel() {
				
					String[] values = new String[] {"ss","ss"};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				scrollPane2.setViewportView(list2);
			}
		}
		{
			JButton listBtn = new JButton("리스트선택보기");
			listBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, list1.getSelectedIndex()+"/"+list1.getSelectedValue());
				}
			});
			contentPanel.add(listBtn);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(100, 100));
			contentPanel.add(scrollPane);
			{
				JList list = new JList();
				scrollPane.setViewportView(list);
			}
			{
				JComboBox comboBox = new JComboBox();
				contentPanel.add(comboBox);
			}
			{
				JComboBox comboBox = new JComboBox();
				contentPanel.add(comboBox);
			}
			{
				String[] data=
					{"수지","혜교","사랑","민교",
					"민슈","열정","콤보","믿음",
					"사랑"
						
					};
			}
		}
		// 라디오 버튼 그룹 생성 및 추가 끝

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

}
